package b1.mobile.blackberry.screen;

import java.util.Vector;

import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;

import net.rim.device.api.i18n.ResourceBundle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;
import b1.mobile.blackberry.entity.ApprovalEntity;
import b1.mobile.blackberry.io.http.ResponseParserHelper;
import b1.mobile.blackberry.io.http.Serviceworker;
import b1.mobile.blackberry.io.http.Serviceworker.ServiceDelegate;
import b1.mobile.blackberry.io.http.SoapContentProvider;
import b1.mobile.blackberry.lang.CommonConstant;
import b1.mobile.blackberry.ui.component.IconListField;
//import b1.mobile.blackberry.ui.component.util.ApprovalsComparator;
import b1.mobile.blackberry.ui.component.util.Capsule;
import b1.mobile.blackberry.ui.component.util.FieldFactory;
import b1.mobile.blackberry.ui.component.util.ThemeUtil;

public class ApprovalListScreen extends BaseFunctionScreen implements ServiceDelegate {

	private static ResourceBundle resources = ResourceBundle.getBundle(BUNDLE_ID, BUNDLE_NAME);
	private IconListField _listField;
	
	Manager bgMgr = new VerticalFieldManager(Field.USE_ALL_WIDTH);
	
	private boolean isLoaded = false;

	/**
	 * 
	 */
	public ApprovalListScreen() {
		setTitle(FieldFactory.makeScreenTitle(resources.getString(TITLE_APPROVAL_LIST)));
		ThemeUtil.decorateScreen(this);
		
		bgMgr.setBackground(BackgroundFactory.createSolidBackground(0x00F3F3F3));
		bgMgr.setMargin(10, 0, 10, 0);
		
		_listField = new IconListField();
		_listField.setListType(CommonConstant.ICON_LIST_TYPE_BS_LIST);
		_listField.setRowHeight(_listField.getRowHeight() * 25 / 10);
		
		bgMgr.add(_listField);
		
		this.add(bgMgr);
		this.add(new LabelField());
	}

	protected void onUiEngineAttached(boolean attached) {
		if (attached && !isLoaded) {
			onLoadList(null);
		}
	}

	protected void makeMenu(Menu menu, int instance) {
	}

	private void onLoadList(Vector listItem) {
		
		/** get approval list from server **/
		String[] names = new String[]{};
		String[] values = new String[]{};
		SoapContentProvider soap = new SoapContentProvider("GetMessageList", names, values);
		
		try{
			Serviceworker worker = new Serviceworker(this, b1.mobile.blackberry.controller.AgentController.getServer(), soap);
			new Thread(worker).start();
		} catch (Exception ex){
			ex.printStackTrace();
		}
	}
	
	protected boolean navigationClick(int status, int time) {
		if (getLeafFieldWithFocus() instanceof IconListField){
			Capsule capsule = (Capsule) _listField.get(_listField, _listField.getSelectedIndex());
			Object value = capsule.value ;
			if (value instanceof ApprovalEntity){
				getUiEngine().pushScreen(new ApprovalDetailScreen(((ApprovalEntity)value).getApprovalId()));
			}
		} else{
			super.navigationClick(status, time);
		}
		return true;
	}
	

	public void onGetResult(Serviceworker worker) {		
		if(worker.responseCode == 200){
			if(worker.getOperator().equals("GetMessageList")){
				bindList(worker.responseMessage);
			}
		}
	}

	private void bindList(String response) {
		ResponseParserHelper help = new ResponseParserHelper(response);
		JSONArray array = (JSONArray)help.parse("GetMessageListResult");

		Vector items = new Vector();
		for(int i = 0; i < array.length(); i++){
			try {
				JSONObject jobj = array.getJSONObject(i);
				ApprovalEntity ety = new ApprovalEntity();
				
				ety.approvalId = jobj.getString("ID");
				ety.approvalFrom = jobj.getString("From");
				ety.approvalDate = jobj.getString("Date");
				ety.approvalShortText = jobj.getString("Subject");
				ety.approvalStatus = jobj.getString("Status");
				ety.approvalWasRead = jobj.getString("WasRead");
				
				items.addElement(new Capsule(ety.getApprovalId(), ety));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		
		final Capsule[] capsules = new Capsule[items.size()];
		items.copyInto(capsules);

		UiApplication.getUiApplication().invokeLater(new Runnable(){
			public void run() {
				_listField.set(capsules);
			}
		});
		
	}
	
	

}
